package com.ft.scan.service.impl;

import com.ft.scan.common.AliNoteUtils;
import com.ft.scan.service.IMobileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class MobileServiceImpl implements IMobileService {

    final AliNoteUtils aliNoteUtils = new AliNoteUtils();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送短信服务
     *
     * @param mobile 电话号
     * @return boolean
     */
    @Override
    public boolean sendNote(String mobile){
        String code = aliNoteUtils.sendNote(mobile);
        if (code == null) {
            System.out.println(mobile+"短信发送失败");
            return false;
        } else {
            System.out.println(mobile+"短信发送成功");
            try {
                redisTemplate.opsForValue().set(mobile, code, 10, TimeUnit.MINUTES);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println(mobile+"插入redis失败");
                return false;
            }
            return true;
        }
    }

    /**
     * 查询验证码
     *
     * @param mobile 电话号
     * @return String
     */
    @Override
    public String selectNoteByMobile(String mobile){
        return (String) redisTemplate.opsForValue().get(mobile);
    }
}
